package com.itheima.controller;

import com.itheima.pojo.SmsHomeNewProduct;
import com.itheima.service.HomeNewProductService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class HomeNewProductController {

    @Autowired
    private HomeNewProductService homeNewProductService;

    /**
     * 添加首页新品
     */
    @PostMapping("/create")
    public ResponseEntity create(@RequestBody List<SmsHomeNewProduct> newProductList){
        homeNewProductService.create(newProductList);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量删除首页新品
     */
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestParam List<Integer> ids){
        homeNewProductService.delete(ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量修改首页新品状态
     */
    @PostMapping("/update/recommendStatus")
    public ResponseEntity updateStatus(@RequestParam Integer recommendStatus,
                                       @RequestParam List<Integer> ids){

        homeNewProductService.updateStatus(recommendStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 修改首页新品排序
     */
    @PostMapping("/update/sort/{id}")
    public ResponseEntity updateSort(@PathVariable Integer id,
                                     @RequestParam Integer sort){
       homeNewProductService.updateSort(id,sort);
       return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 分页查询首页新品
     */
    @GetMapping("/list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String productName,
                               Integer recommendStatus){
        PageResultVo<SmsHomeNewProduct> prv = homeNewProductService.list(pageNum,pageSize,productName,recommendStatus);
        return ResponseEntity.ok(Result.success(prv));
    }

}
